import requests,pprint


def login(username,password):
    payload={
        'username':username,
        'password':password
    }
    r=requests.post("http://localhost/api/mgr/loginReq",data=payload)
    retobj = r.json()
    pprint.pprint(retobj)
    # print(r.cookies['sessionid'])
    return retobj,r.cookies['sessionid']


def list_course(sessionid):
    params = {
        'action':'list_course',
        'pagenum':'1',
        'pagesize':20
    }
    response = requests.get("http://localhost/api/mgr/sq_mgr/",  params=params,cookies={'sessionid':sessionid})
    # 获取结果，返回给调用者
    request1=response.request.headers#获取请求头内容
    request1body=response.request.body#获取请求消息内容
    retDict = response.json()
    # pprint.pprint(retDict)
    print(type(retDict))
    return retDict,request1
# list_course()


#添加课程
def add_course(name,desc,idx,sessionid):
    payload={
        'action': 'add_course',
        'data':'''{
        "name":"%s",
        "desc":"%s",
        "display_idx":"%s"
        }'''%(name,desc,idx)
    }

    r=requests.post("http://localhost/api/mgr/sq_mgr/",data=payload,cookies={'sessionid': sessionid})
    retobj=r.json()
    pprint.pprint(retobj)
    return retobj
# add_course('python2','python课程','3','u6vrieh0rsd8clmi0c5odfavrknzeu0o')


#删除课程
def delete_course(id,sessionid):
    payload={
        'action':'delete_course',
        'id'    :id

    }

    r=requests.delete('http://localhost/api/mgr/sq_mgr/',data=payload,cookies={'sessionid': sessionid})
    retobj=r.json()
    pprint.pprint(retobj)
    return retobj
# delete_course(1347)

#修改课程
def modify_course(cid,name,desc,idx,sessionid):
    payload={
        'action':'modify_course',
        'id'    :cid,
        'newdata':'''{
        "name":"%s",
        "desc":"%s",
        "display_idx":"%s"
        } '''%(name,desc,idx)
    }

    r=requests.put('http://localhost/api/mgr/sq_mgr/',data=payload,cookies={'sessionid': sessionid})
    retobj=r.json()
    pprint.pprint(retobj)
    return retobj

if __name__ == '__main__':
    # 1- 登录操作,获取登录里面的sessionid
    res=login('auto','sdfsdfsdf')
    print(res)
    #
    #2- 列出课程
    # print(list_course(res))






